<template>
	<view>
		<view class="con">
			<view class="uni-form-item uni-column">
				<input type="text" class="uni-input" name="" v-model="name" placeholder="请填写姓名" />
			</view>
			<view class="uni-form-item uni-column">
				<input type="tel" class="uni-input" name="" v-model="phone" placeholder="请输入手机号" />
			</view>
			<view class="uni-form-item uni-column column-with-btn">
				<input type="number" class="uni-input" name="" v-model="vc" placeholder="请输入验证码" />
				<button :class="{active : !disableCodeBtn}"  @click="getCode">{{codeBtn.text}}</button>
			</view>
			<!-- #ifdef H5 -->
			<button type="primary" @click="sendVc">免费注册</button>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<!-- <button type="primary" @tap="getUserProfile">授权登录</button> -->
			<view class="tel" @tap="getUserProfile">立即注册</view> 
			<!-- #endif -->
			
		</view>
	</view>
</template>

<script>
	let App = getApp();
	export default {
		data() {
			return {
				name:'',
				seconds: 120,
				codeBtn: {
					text: '获取验证码',
					waitingCode: false,
					count: this.seconds
				},
				phone:'',
				vc:'',
				mobile:'',
				timer:'',
				pid:0,
				friend:'',
			}
		},
		onUnload() {
			if(this.timer) {  
				clearTimeout(this.timer);  
				this.timer = null;  
			}  
		},
		
		onLoad(optsions) {
			const scene = decodeURIComponent(optsions.scene)
			console.log(scene);
			this.pid = scene;
			this.getOpenid();
		},
		
		methods: {
			getUserProfile() {
			    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
			    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
			    wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						console.log(res)
						if (res.errMsg === "getUserProfile:ok") {
							this.sendVc(res.userInfo.avatarUrl,res.userInfo.nickName)
							
						}
					}
			    })
			},
			//获取openid
			getOpenid(){
				let backData
				uni.login({
					provider: 'weixin',
					success :(res)  => {
						this.code = res.code
					}
				});
			},
			getUrlParam(name) {
					var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
					let url = window.location.href.split('#')[0]
					let search = url.split('?')[1]
					if (search) {
						var r = search.substr(0).match(reg)
						if (r !== null){
							return unescape(r[2])
						}	
						return null
					} else{
						return null
					}	
				},
				
			getCode: function () {
				let name = this.name
				let phone = this.phone
				var reg = /^1[3456789]\d{9}$/;
				if (phone == "") {
					uni.showToast({
						title: "手机号不能为空",
						icon: "none"
					})
				} else if (!reg.test(phone)) {
					uni.showToast({
						title: "请输入有效手机号",
						icon: "none"
					})
				} else if(name == ""){
					uni.showToast({
						title: "姓名不能为空",
						icon: "none"
					})
				} else {
					
					this.$api.interfaceApi('sendSms')({
						'mobile': phone,
						'admin_id':App.globalData.sys
					}).then(res=>{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
						
					})
		
					this.codeBtn.waitingCode = true;
					this.codeBtn.count = this.seconds;
					this.codeBtn.text = this.codeBtn.count + 's';
					
					let countdown = setInterval( () => {
						this.codeBtn.count--;
						this.codeBtn.text = this.codeBtn.count + 's';
						if( this.codeBtn.count < 0 ){
							clearInterval(countdown);
							this.codeBtn.text = '重新发送';
							this.codeBtn.waitingCode = false;
						}
					},1000);
				}	
			},
			
			async sendVc(head_img,nickName){
				let vc = this.vc
				let phone = this.phone
				let name = this.name
				var reg = /^1[3456789]\d{9}$/;
				console.log(name);
				if (name == "") {
					uni.showToast({
						title: "姓名不能为空",
						icon: "none"
					})
					return;
				}
				
				if (phone == "") {
					uni.showToast({
						title: "手机号不能为空",
						icon: "none"
					})
					return;
				} else if (!reg.test(phone)) {
					uni.showToast({
						title: "请输入有效手机号",
						icon: "none"
					})
					return;
				} 
				if (vc == "") {
					uni.showToast({
						title: "请填入验证码",
						icon: "none"
					})
					return;
				}
				
				let result = await this.$api.interfaceApi('sendVc')({
					'vc': vc,
					'mobile': phone,
					'code':this.code,
					"headImg":head_img,
					"nickname":nickName,
					'name':name,
					"pid":this.pid,
					'admin_id':App.globalData.sys
				})
				if (result.code == 200) {
					uni.setStorageSync('userinfo',result.data)
					uni.showToast({
						title: result.msg,
						icon:'none'
					})
					
					setTimeout(() => {
						uni.switchTab({
							url:'/pages/index/index'
						})
					}, 1000);
					
				}else if(result.code == 201){
					uni.showToast({
						title: result.msg,
						icon:'none'
					})
					
					setTimeout(() => {
						uni.switchTab({
							url:'/pages/index/index'
						})
					}, 1000);
				}else{
					uni.showToast({
						title: result.msg,
						icon:'none'
					})
				}
			}
		
			
		}
	}
</script>

<style lang="scss" scoped>

		.tel{
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			color: #fff;
			background-color: #00aaff;
			border-radius: 40rpx;
		} 
			
		$color-primary: #00aaff;
		body{
			background-color: #FFFFFF;
		}
		.con{
			width: 99%;
			margin: 10rpx;
			background-color: #FFFFFF;
			padding: 40rpx;
			box-sizing: border-box;
			border-radius: 30rpx;
		}
		
		.uni-form-item{
			margin-bottom: 40upx;
			padding: 0;
			border-bottom: 1px solid #e3e3e3;
			}
			.uni-input{
				font-size: 30upx;
				padding: 7px 10rpx;
			}
		
		.column-with-btn{
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
		}
			button{
				font-size: 24upx;
				margin: 0;
				text-align: center;
			}
				button:after{
					border: none
				}
				button.active{
					background-color: $color-primary;
					color: $uni-text-color-inverse;
				}
		
</style>
